﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkitMvvmDemo.Models;
using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using MessageBox = System.Windows.MessageBox;

namespace CommunityToolkitMvvmDemo.ViewModels
{
    public partial class CourseCenterViewModel : ObservableObject
    {

        [ObservableProperty]
        public ObservableCollection<Person> categoryCourse;
        [ObservableProperty]
        public ObservableCollection<Person> categoryTechnology;
        [ObservableProperty]
        public ObservableCollection<Person> categoryTeacher;

        [ObservableProperty]
        public ObservableCollection<Person> courseList;

        [ObservableProperty]
        public string  loading = "Visible";

        public CourseCenterViewModel()
        {
            CategoryCourse = new ObservableCollection<Person>();
            CategoryCourse.Add(new Person { CategoryName = "全部", IsSelected = true });
            CategoryCourse.Add(new Person { CategoryName = "公开课", IsSelected = false });
            CategoryCourse.Add(new Person { CategoryName = "VIP课程", IsSelected = false });

            CategoryTechnology = new ObservableCollection<Person>();
            CategoryTechnology.Add(new Person { CategoryName = "全部", IsSelected = true });
            CategoryTechnology.Add(new Person { CategoryName = "C#", IsSelected = false });
            CategoryTechnology.Add(new Person { CategoryName = "Java", IsSelected = false });
            CategoryTechnology.Add(new Person { CategoryName = "WPF", IsSelected = false });
            CategoryTechnology.Add(new Person { CategoryName = "Vue", IsSelected = false });
            CategoryTechnology.Add(new Person { CategoryName = "上位机", IsSelected = false });
            CategoryTechnology.Add(new Person { CategoryName = "微服务", IsSelected = false });

            CategoryTeacher = new ObservableCollection<Person>();
            CategoryTeacher.Add(new Person { CategoryName = "全部", IsSelected = true });
            CategoryTeacher.Add(new Person { CategoryName = "玄奘", IsSelected = false });
            CategoryTeacher.Add(new Person { CategoryName = "鉴真", IsSelected = false });
            CategoryTeacher.Add(new Person { CategoryName = "星云", IsSelected = false });
            CategoryTeacher.Add(new Person { CategoryName = "虚云", IsSelected = false });



            CourseList = new ObservableCollection<Person>();

            Task.Run(async () =>
            {
                await Task.Delay(2000);
                Loading = "Hidden";
                Application.Current.Dispatcher.Invoke(() =>
                {
                    CourseList.Add(new Person
                    {
                        CourseName = "鸡鸣寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "古鸡鸣寺位于鸡笼山东麓，是南京最古老梵刹之一。在西晋永康元年（300年）就曾在此倚山造室，始创道场，南朝梁普通八年（527年），在鸡笼山创建寺院。从此成为千余佛教圣地。",
                        Teachers = new List<string> { "玄奘", "鉴真", "虚云" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "灵谷寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "灵谷寺坐落在风景秀丽的钟山东南麓，始建于梁天监十三年（公元515年），为全国重点开放寺庙之一，自七十年代开放以来，已接待了成千上万的游人、香客，并与海内外开展了广泛的佛教文化交流活动。",
                        Teachers = new List<string> { "玄奘", "鉴真" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "栖霞寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "南京栖霞古寺位于南京市栖霞区栖霞山，南齐永明二年（公元484年），南朝隐士、著名经学家明僧绍隐居摄山（即栖霞山），舍宅为寺，法度禅师以栖霞精舍为基础，正式创立栖霞寺；清朝乾隆年间五次南巡，均驻跸栖霞寺。",
                        Teachers = new List<string> { "虚云" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "毗卢寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "毗卢寺位于汉府街4号的毗卢寺，始建于明嘉靖年间，是金陵名刹之一。民国时期成为全国佛教的中心，中国佛教会、中华佛学研究会、中国宗教联谊会、首都中医院皆设于此。",
                        Teachers = new List<string> { "玄奘" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "清凉寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "南京市清凉山清凉寺，始建于唐中和四年（公元884年），是中国佛教禅宗五家之一——法眼宗的发源地。\r\n",
                        Teachers = new List<string> { "玄奘" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "高座寺",
                        Url = "www.baidu.com",
                        Cover = "/Assets/Images/room2.jpg",
                        Description = "高座寺位于南京城南中华门外的雨花台。始建于东晋初年，原名为“甘露寺”。",
                        Teachers = new List<string> { "鉴真" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "鹭峰寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "鹫峰寺坐落于南京白鹭洲公园东北角，建于明代天顺五年(1461年)，为纪念唐朝名僧鹫峰而得名，为南京市文物保护单位。",
                        Teachers = new List<string> { "虚云" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "瓦官寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "瓦官寺位于南京中华门内西南隅的花露岗。始建于东晋兴宁二年（364年）。瓦官寺和凤凰台一带是南京文脉之地，六朝风物俯拾皆是。",
                        Teachers = new List<string> { "虚云" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "金粟庵",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "金粟庵与现瓦官寺隔街相望，为后人在瓦官寺遗址上修建的大小寺庵之一。取顾恺之绘维摩诘示疾壁画的典故，因维摩诘居士乃金粟如来应世，故称金粟庵。",
                        Teachers = new List<string> { "星云" }
                    });
                    CourseList.Add(new Person
                    {
                        CourseName = "泰山寺",
                        Cover = "/Assets/Images/room1.jpg",
                        Url = "www.baidu.com",
                        Description = "泰山寺供奉大、小金装菩萨27座和16幅尊者画像，还有质地缅玉的高1.2米坐佛1尊，泰山老姥和使女神像各1座，其中以泰山老母碧霞元君为主。",
                        Teachers = new List<string> { "星云" }
                    });
                });
            });
        }

        [RelayCommand]
        private void Load(object sender)
        {
            userControl = sender as UserControl;
            //MessageBox.Show(userControl.Name.ToString());
        }

        [RelayCommand]
        private void OpenUrl(string url)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("chrome.exe");
            startInfo.Arguments = url;
            startInfo.UseShellExecute = true;
            Process.Start(startInfo);
        }

        private UserControl userControl;

        [RelayCommand]
        private void RadioButton(object sender)
        {
            var rb1 = sender as RadioButton;
            //MessageBox.Show(rb1.Content.ToString());

            var obj1 = (FrameworkElement)userControl.FindName("CourseListSource");
            var view = CollectionViewSource.GetDefaultView((obj1 as ItemsControl).ItemsSource);

            if (rb1 != null && rb1.Content.ToString() == "全部")
            {
                view.Filter = null;
                //排序
                view.SortDescriptions.Add(new SortDescription("CourseName", ListSortDirection.Ascending));
            }
            else
            {
                view.Filter = new Predicate<object>((o) =>
                {
                    return (o as Person).Teachers.Exists(t => t == rb1.Content);
                });
            }
        }
    }
}
